package com.sudichina.sudichina.view.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.sudichina.sudichina.NET.Api;
import com.sudichina.sudichina.NET.NetUtils;
import com.sudichina.sudichina.NET.onRequestDataFinish;
import com.sudichina.sudichina.R;
import com.sudichina.sudichina.bean.Result;
import com.sudichina.sudichina.utils.DESMD5Utils;
import com.sudichina.sudichina.utils.GetSystem;
import com.sudichina.sudichina.utils.Toastutils;
import com.sudichina.sudichina.utils.UiUtils;
import com.sudichina.sudichina.view.activity.main.BaseActivity;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ${杨伟乔} on 2017/3/8.
 * 邮箱: yangweiqiao@126.com
 * QQ: 463217558
 * 别人光鲜的背后或者有着太多不为人知的痛苦
 * 学习可以改变一切、 行动可以解决一切问题、
 */
public class ForgetPswActivity extends BaseActivity implements onRequestDataFinish {


    @BindView(R.id.iv_back)
    ImageView privateIvBack;
    @BindView(R.id.tv_title)
    TextView privateTvTitle;
    @BindView(R.id.tv_right)
    TextView privateTvRight;
    @BindView(R.id.et_phone_no)
    EditText privateEtPhoneNo;
    @BindView(R.id.et_verification_code)
    EditText privateEtVerificationCode;
    @BindView(R.id.btn_get_verification_code)
    Button privateBtnGetVerificationCode;
    @BindView(R.id.et_pasd)
    EditText privateEtPasd;
    @BindView(R.id.et_pasd2)
    EditText privateEtPasd2;
    @BindView(R.id.btn_continue)
    Button privateBtnContinue;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_forgot_pwd);
        ButterKnife.bind(this);
        privateTvTitle.setText("密码重置");

    }


    String phone;

    @OnClick({R.id.iv_back, R.id.btn_get_verification_code, R.id.btn_continue})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                //关闭当前的界面
                finish();
                break;
            case R.id.btn_get_verification_code:
                phone = privateEtPhoneNo.getText().toString().trim();

                if (GetSystem.isMobileNO(phone)) { //正则判断
                    /**
                     * 发送验证码
                     */
                    showGetVerificationCodeState();
                    sendCode();
                } else {
                    Toastutils.showToast("手机格式不正确");
                }


                break;
            case R.id.btn_continue:
                if (!privateEtPasd.getText().toString().trim().equals(privateEtPasd2.getText().toString().trim())) {
                    UiUtils.showToast("两次输入的密码不一致");
                    return;
                }


                //效验验证码
                String code = privateEtVerificationCode.getText().toString().trim();
                HashMap<String, Object> map = new HashMap<>();
/*
    "authTarget":"15321859523",
                "authType":"sms_forget_verify",
                "authCode":"011231"
 */
                map.put("authTarget", phone);
                map.put("authType", "sms_forget_verify");
                map.put("authCode", code);
                Type type = new TypeToken<Result>() {
                }.getType();
                NetUtils.requestData(Api.EQUALS_CODE, map, this, type, false);


                break;
        }
    }


    @Override
    public void requestdataFinish(String catalog, Object bean) {

        switch (catalog) {

            case Api.SEND_CODE:
                Result result = (Result) bean;

                int code = result.getCode();
                if (code == 1) {
                    UiUtils.showToast("验证码发送成功");
                }
                if (code == 8005) {
                    UiUtils.showToast("验证码发送失败");
                }

                break;


            case Api.EQUALS_CODE:

                Result coderesult = (Result) bean;
                int code1 = coderesult.getCode();
                if (code1 == 1 || coderesult.getMsg().equals("验证码通过！")) {


                    //成功后开始修改密码
                    HashMap map = new HashMap<String, Object>();

                    map.put("accountName", phone);
                    map.put("accountPassword", DESMD5Utils.MD5(privateEtPasd.getText().toString().trim(), false));
                    map.put("accountType", 0);
                    map.put("source", 0);
                    Type type = new TypeToken<Result>() {
                    }.getType();
                    NetUtils.requestData(Api.FORGOT, map, this, type, false);
                }
                if (code1 == 8007) {
                    Toastutils.showToast("验证码失效");
                }


                break;

            case Api.FORGOT:
                Result bean1 = (Result) bean;
                int code2 = bean1.getCode();

                String msg = bean1.getMsg();
                if (code2 == 8009) {
                    Toastutils.showToast(msg);

                    return;
                }
                if (code2 == 1) {

                    startActivity(new Intent(this, LoginActivity.class));
                    finish();
                }

                //修改成功

                Toastutils.showToast(msg);


                break;

        }
    }

    @Override
    public void requestdataFailed(IOException e) {

    }


    private static final int TIME_INTERVAL = 60;
    private int one_minute = TIME_INTERVAL;
    private Handler handler = new Handler();

    public void showGetVerificationCodeState() {
        privateBtnGetVerificationCode.setEnabled(false);
        handler.post(new Runnable() {
            @Override
            public void run() {
                privateBtnGetVerificationCode.setText(--one_minute < 0 ? "获取验证码" :
                        String.valueOf(one_minute).concat("秒"));
                if (one_minute < 0) {
                    one_minute = TIME_INTERVAL;
                    privateBtnGetVerificationCode.setEnabled(true);
                    return;
                }
                handler.postDelayed(this, 1000);
            }
        });
    }

    private void sendCode() {


        HashMap<String, Object> map = new HashMap<>();
/*
"authTarget":"15321859523",
"authType":"sms_forget_verify",
"authSource":"Android"
 */
        map.put("authTarget", phone);
        map.put("authType", "sms_forget_verify");
        map.put("authSource", "Android");

        Type type = new TypeToken<Result>() {
        }.getType();
        NetUtils.requestData(Api.SEND_CODE, map, this, type, false);
    }
}
